﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.IO;
using System.Threading;

class Program
{
    static void Main()
    {
        string sourceFolder = "D:\\MySQL\\bin";
        string targetFolder = "E:\\DE";

        // 创建目标文件夹，如果它不存在
        if (!Directory.Exists(targetFolder))
        {
            Directory.CreateDirectory(targetFolder);
        }

        string[] filesToCopy = Directory.GetFiles(sourceFolder);
        int numberOfThreads = Environment.ProcessorCount; // 使用可用的CPU核心数作为线程数

        // 创建线程数组
        Thread[] threads = new Thread[numberOfThreads];

        for (int i = 0; i < numberOfThreads; i++)
        {
            int threadIndex = i;
            threads[i] = new Thread(() =>
            {
                for (int j = threadIndex; j < filesToCopy.Length; j += numberOfThreads)
                {
                    string sourceFile = filesToCopy[j];
                    string targetFile = Path.Combine(targetFolder, Path.GetFileName(sourceFile));

                    // 执行文件拷贝
                    File.Copy(sourceFile, targetFile, true);
                    Console.WriteLine($"拷贝文件 {sourceFile} 到 {targetFile}");
                }
            });

            threads[i].Start();
        }

        // 等待所有线程完成
        foreach (var thread in threads)
        {
            thread.Join();
        }

        Console.WriteLine("拷贝完成");
    }
}
